// @ts-ignore
/* eslint-disable */
import { request } from 'umi';
import { PlanBody, PlanListItem } from './data';
import { Key } from 'react';

export async function dataList(params: {
  // query
  /** 当前的页码 */
  current?: number;
  /** 页面的容量 */
  pageSize?: number;
}) {
  return request<{
    data: PlanListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>('/v2/enterprise/train/plan/statistic', {
    method: 'GET',
    params: {
      ...params,
      pageNum: params.current,
      orderByColumn: 'createTime',
      isAsc: 'desc',
    },
  });
}

export async function orgDataList(params: { planId?: number }) {
  return request<{
    data: PlanListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>('/v2/enterprise/train/plan/statistic/' + params.planId + '/org', {
    method: 'GET',
  });
}

export async function loadData(param: PlanBody): Promise<{ data: PlanBody }> {
  return request(`/v2/enterprise/train/plan/${param.planId}`, { method: 'GET' });
}

export async function loadTaskData(param: PlanBody): Promise<{ data: PlanBody }> {
  return request(`/v2/enterprise/train/plan/task/employee/${param.taskId}`, { method: 'GET' });
}
export async function loadRecordData(param: PlanBody): Promise<{ data: PlanBody }> {
  return request(`/v2/enterprise/train/plan/task/record/${param.taskId}`, { method: 'GET' });
}
export async function loadTaskQualification(param: PlanBody): Promise<{ data: PlanBody }> {
  return request(`/v2/enterprise/train/plan/task/qualification/${param.taskId}`, { method: 'GET' });
}

export async function addData(param: PlanBody): Promise<{ data: number }> {
  return request('/v2/enterprise/train/plan', { method: 'POST', data: param });
}

export async function updateData(param: PlanBody): Promise<{ data: number }> {
  return request('/v2/enterprise/train/plan', { method: 'PUT', data: param });
}

export async function updateCourse(param: PlanBody): Promise<{ data: number }> {
  return request('/v2/enterprise/train/plan/course', { method: 'PUT', data: param });
}

export async function updatePostpone(param: PlanBody): Promise<{ data: number }> {
  return request('/v2/enterprise/train/plan/task/postpone', { method: 'POST', data: param });
}

export async function removeData(data: number[], options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/train/plan/` + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function removeQuestionData(
  dataId: number,
  data: number[],
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`/v2/enterprise/train/plan/${dataId}/` + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function loadSelectTag() {
  return request(`/v2/enterprise/train/plan/tags`, { method: 'GET' });
}

export async function download() {
  request(`/v2/enterprise/train/plan/question/template`, {
    method: 'POST',
    // 必须加responseType: 'blob',
    responseType: 'blob',
  }).then((res) => {
    const objectURL = URL.createObjectURL(res);
    let btn = document.createElement('a');
    btn.download = '试题模板.xlsx'; //文件类型
    btn.href = objectURL;
    btn.click();
    URL.revokeObjectURL(objectURL);
    // @ts-ignore
    btn = null;
  });
}

export async function questionList(dataId: number) {
  return request(`/v2/enterprise/train/plan/question/${dataId}`, {
    method: 'GET',
  });
}

export async function coursewareListSave(dataId: number, ids: Key[]) {
  return request(`/v2/enterprise/train/plan/courseware/save/${dataId}`, {
    data: ids,
    method: 'POST',
  });
}

export async function coursewareList(dataId: number) {
  return request(`/v2/enterprise/train/plan/courseware/${dataId}`, {
    method: 'GET',
  });
}

export async function employeeListSave(dataId: number, ids: Key[]) {
  return request(`/v2/enterprise/train/plan/employee/save/${dataId}`, {
    data: ids,
    method: 'POST',
  });
}

export async function employeeList(dataId: number) {
  return request(`/v2/enterprise/train/plan/employee/${dataId}`, {
    method: 'GET',
  });
}

export async function publishData(data: number, options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/train/plan/publish/` + data, {
    data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function unPublishData(data: number, options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/train/plan/un-publish/` + data, {
    data,
    method: 'POST',
    ...(options || {}),
  });
}
